// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import zombie.util.StringUtils;

public class RCONClient {
    private Socket socket;

    public boolean disconnect() {
        try {
            this.socket.close();
            return true;
        } catch (IOException iOException) {
            System.out.println("Disconnect failed: " + iOException.getMessage());
            return false;
        }
    }

    public boolean connect(String ip, String port) {
        try {
            this.socket = new Socket();
            this.socket.setSoTimeout(5000);
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, Integer.parseInt(port));
            this.socket.connect(inetSocketAddress, 1000);
            return true;
        } catch (IOException iOException) {
            System.out.println("Connect failed: " + iOException.getMessage());
            return false;
        }
    }

    public boolean auth(String password) {
        try {
            int _int = (int)(65535L & System.currentTimeMillis());
            RCONClient.RCONMessage rCONMessage0 = new RCONClient.RCONMessage(_int, 3, password);
            rCONMessage0.writeObject(this.socket.getOutputStream());
            RCONClient.RCONMessage rCONMessage1 = new RCONClient.RCONMessage();
            rCONMessage1.readObject(this.socket.getInputStream(), 14);
            if (rCONMessage1.type == 0 && rCONMessage1.id == _int) {
                RCONClient.RCONMessage rCONMessage2 = new RCONClient.RCONMessage();
                rCONMessage2.readObject(this.socket.getInputStream(), 14);
                if (rCONMessage2.type == 2 && rCONMessage1.id == _int) {
                    return true;
                } else {
                    System.out.println("Authentication failed: auth response");
                    return false;
                }
            } else {
                System.out.println("Authentication failed: response value");
                return false;
            }
        } catch (IOException iOException) {
            System.out.println("Authentication failed: timeout");
            return false;
        }
    }

    public String exec(String command) {
        try {
            int _int = (int)(65535L & System.currentTimeMillis());
            RCONClient.RCONMessage rCONMessage0 = new RCONClient.RCONMessage(_int, 2, command);
            rCONMessage0.writeObject(this.socket.getOutputStream());
            RCONClient.RCONMessage rCONMessage1 = new RCONClient.RCONMessage();
            rCONMessage1.readObject(this.socket.getInputStream(), 0);
            return new String(rCONMessage1.body);
        } catch (IOException iOException) {
            System.out.println("Command execution failed");
            return null;
        }
    }

    public boolean send(String url, String text) {
        try {
            HttpClient httpClient = HttpClient.newHttpClient();
            HttpRequest httpRequest = HttpRequest.newBuilder()
                .setHeader("Content-type", "application/json")
                .uri(URI.create(url))
                .POST(BodyPublishers.ofString("{\"text\":\"" + text + "\"}"))
                .build();
            HttpResponse httpResponse = httpClient.send(httpRequest, BodyHandlers.ofString());
            if (httpResponse != null && httpResponse.statusCode() != 200) {
                System.out.println((String)httpResponse.body());
                return false;
            } else {
                return true;
            }
        } catch (Exception exception) {
            System.out.println("Result post failed");
            return false;
        }
    }

    private static void sleep(long _long) {
        try {
            Thread.sleep(_long);
        } catch (Exception exception) {
        }
    }

    public static void main(String[] string5) {
        String string0 = null;
        String string1 = null;
        String string2 = null;
        String string3 = null;
        String string4 = null;
        boolean _boolean = false;

        for (int _int = 0; _int < string5.length; _int++) {
            if (!StringUtils.isNullOrEmpty(string5[_int])) {
                if (string5[_int].equals("-ip")) {
                    string0 = string5[++_int].trim();
                } else if (string5[_int].equals("-port")) {
                    string1 = string5[++_int].trim();
                } else if (string5[_int].equals("-password")) {
                    string2 = string5[++_int].trim();
                } else if (string5[_int].equals("-command")) {
                    string3 = string5[++_int].trim();
                } else if (string5[_int].equals("-webhook")) {
                    string4 = string5[++_int].trim();
                }
            }
        }

        if (!StringUtils.isNullOrEmpty(string0)
            && !StringUtils.isNullOrEmpty(string1)
            && !StringUtils.isNullOrEmpty(string2)
            && !StringUtils.isNullOrEmpty(string3)) {
            if (!StringUtils.isNullOrEmpty(string4)) {
                _boolean = true;
            }

            RCONClient rCONClient = new RCONClient();

            do {
                if (rCONClient.connect(string0, string1)) {
                    if (rCONClient.auth(string2)) {
                        if (_boolean && !rCONClient.send(string4, String.format("Connected to server %s:%s", string0, string1))) {
                            break;
                        }

                        String string6 = null;

                        do {
                            String string7 = rCONClient.exec(string3);
                            if (StringUtils.isNullOrEmpty(string7)) {
                                break;
                            }

                            if (!string7.equals(string6)) {
                                if (_boolean) {
                                    if (!rCONClient.send(string4, string7)) {
                                        break;
                                    }

                                    sleep(5000L);
                                } else {
                                    System.out.println(string7);
                                }
                            }

                            string6 = string7;
                        } while (_boolean);

                        if (_boolean && !rCONClient.send(string4, "Connection to server lost")) {
                            break;
                        }
                    }

                    rCONClient.disconnect();
                }

                if (_boolean) {
                    sleep(60000L);
                }
            } while (_boolean);
        } else {
            System.out.println("Incorrect arguments");
        }
    }

    private static class RCONMessage {
        private static final byte[] input = new byte[4096];
        private static final ByteBuffer bbr = ByteBuffer.wrap(input);
        private static final byte[] output = new byte[4096];
        private static final ByteBuffer bbw = ByteBuffer.wrap(output);
        static final int baseSize = 10;
        int size;
        int id;
        int type;
        byte[] body;

        RCONMessage() {
        }

        RCONMessage(int int0, int int1, String string) throws UnsupportedEncodingException {
            this.id = int0;
            this.type = int1;
            this.body = string.getBytes();
            this.size = 10 + string.length();
        }

        private void writeObject(OutputStream outputStream) throws IOException {
            bbw.putInt(this.size);
            bbw.putInt(this.id);
            bbw.putInt(this.type);
            bbw.put(this.body);
            bbw.put((byte)0);
            bbw.put((byte)0);
            outputStream.write(output, 0, this.size + 4);
            bbw.clear();
        }

        private void readObject(InputStream inputStream, int _int) throws IOException {
            if (_int == 0) {
                inputStream.read(input);
            } else {
                inputStream.read(input, 0, _int);
            }

            this.size = bbr.getInt();
            this.id = bbr.getInt();
            this.type = bbr.getInt();
            if (this.size > 10) {
                this.body = new byte[this.size - 10];
                bbr.get(this.body, 0, this.size - 10);
            }

            bbr.get();
            bbr.get();
            bbr.clear();
        }

        static {
            bbr.order(ByteOrder.LITTLE_ENDIAN);
            bbw.order(ByteOrder.LITTLE_ENDIAN);
        }
    }
}
